﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using Scavenger.VectorStuff;
namespace Scavenger
{
    class VectorMath
    {

        public static float DotProduct(Vector2 v1, Vector2 v2)
        {
            float Value = v1.X * v2.X + v1.Y * v2.Y;
            return Value;
        }

        public static Vector3 CrossProduct(Vector3 v1, Vector3 v2)
        {
            Vector3 Vector = new Vector3(v1.Y * v2.Z - v1.Z * v2.Y, v2.X * v1.Z - v2.Z * v1.X, v1.X * v2.Y - v1.Y * v2.X);
            return Vector;
        }

        public static Vector2 LineNormal(Vector2 v1)
        {
            Vector2 Normal = new Vector2(-v1.Y, v1.X);
            return Normal;
        }

        public static Vector2 PointsToVector(Vector2 v1, Vector2 v2)
        {
            Vector2 Vector = v2 - v1;
            return Vector;
        }

        public static float Determinant2D(Vector2 v1, Vector2 v2)
        {
            float Determinant = v1.X * v2.Y - v1.Y * v2.X;
            return Determinant;
        }

        public static Vector2 Intersection(Line line1, Line line2)
        {
            Vector2 Intersection;
            float Determinant = Determinant2D(line1.Normal, line2.Normal);

            if (Determinant == 0)
            { Intersection = new Vector2(-1); }
            else if (Determinant == 1 || Determinant == -1)
            { Intersection = line1.ClosestPointOnLine(line2.PointStart); }
            else
            {
                float Distance = line1.DistanceToLine(line2.PointStart);
                float DotProduct = VectorMath.DotProduct(-line1.Normal, line2.DirectionVector);
                float Steps = Distance / DotProduct;
                Intersection = line2.PointStart + line2.DirectionVector * Steps;
            }

            return Intersection;

        }

        public static Vector2 Intersection(Vector vector, Line line)
        {
            Vector2 Intersection;
            float Determinant = Determinant2D(vector.Normal, line.Normal);

            if (Determinant == 0)
            { Intersection = new Vector2(-1); }
            else
            {
                if (Determinant == 1 || Determinant == -1)
                { Intersection = vector.ClosestPointOnLine(line.PointStart); }
                else
                {
                    float Distance = vector.DistanceToLine(line.PointStart);
                    float DotProduct = VectorMath.DotProduct(-vector.Normal, line.DirectionVector);
                    float Steps = Distance / DotProduct;
                    Intersection = line.PointStart + line.DirectionVector * Steps;
                }

                if (Math.Round(VectorMath.PointsToVector(Intersection, vector.PointStart).Length()
                     + VectorMath.PointsToVector(Intersection, vector.PointEnd).Length()) > Math.Round(vector.Length))
                {
                    Intersection = new Vector2(-1);
                }

            }

            return Intersection;

        }

        public static Vector2 Intersection(Vector vector1, Vector vector2)
        {
            Vector2 Intersection;
            float Determinant = Determinant2D(vector1.Normal, vector2.Normal);

            if (Determinant == 0)
            { Intersection = new Vector2(-1); }
            else
            {
                if (Determinant == 1 || Determinant == -1)
                { Intersection = vector1.ClosestPointOnLine(vector2.PointStart); }
                else
                {
                    float Distance = vector1.DistanceToLine(vector2.PointStart);
                    float DotProduct = VectorMath.DotProduct(-vector1.Normal, vector2.DirectionVector);
                    float Steps = Distance / DotProduct;
                    Intersection = vector2.PointStart + vector2.DirectionVector * Steps;
                }

                if ((Math.Round(VectorMath.PointsToVector(Intersection, vector1.PointStart).Length()
                     + VectorMath.PointsToVector(Intersection, vector1.PointEnd).Length()) > Math.Round(vector1.Length)) ||
                    (Math.Round(VectorMath.PointsToVector(Intersection, vector2.PointStart).Length()
                        + VectorMath.PointsToVector(Intersection, vector2.PointEnd).Length()) > Math.Round(vector2.Length)))
                {
                    Intersection = new Vector2(-1);
                }

            }

            return Intersection;

        }
    }
}
